package commonInfrastructure.accountManagement.session;

import frm.billingAndPaymentProcessing.entity.InvoiceEntity;
import java.util.Properties;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;

@Stateless
@LocalBean
public class EmailSessionBean{


       
    String emailServerName = "smtp.gmail.com";
    public EmailSessionBean() {
    }

    public void emailInitialPassward(String toEmailAdress,String initialPassword) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getInstance(props,
			new javax.mail.Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("melionfood.m04","weloveyoutwk");
				}
			});
 
		try {
 
                    
                    
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("melionfood.m04@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(toEmailAdress));
			message.setSubject("Initial Password");
			message.setText("Greeting from Melion Food limited!" +
					"\n\n Here is your initial password:"+initialPassword);
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
    public void sendDunningEmail(String toEmailAddress, InvoiceEntity invoiceEntity){
        Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getInstance(props,
			new javax.mail.Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("melionfood.m04","weloveyoutwk");
				}
			});
 
		try {
 
                    
                    
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("melionfood.m04@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(toEmailAddress));
                        
			message.setSubject("Dunning Email");
			message.setText("Dear Customer" + invoiceEntity.getDeliveryOrder().getParentSalesOrder().getCustomer().getName() + ":" +
					"\n\n This is a reminding Email."+"\n Your company ordered goods have been shipped, you may check the invoice we send to you previously."+ 
                                         "\n However, we have not received any of your payment until now, please check with your finance department"
                                         + "\n Your Company has onwed us total of " + (0 - invoiceEntity.getPayment_balance()) + ", and please make the payment as soon as possible"
                                        + "\n\n\n Thanks," + "\n Merlion Food limieted"
                                );
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
    }
}
    
    
    
    
    
